<?php
/**
 * 
 * 
 * @version $Id: Tmall.php 484 2012-11-05 07:08:13Z linsir123 $
 */
class controllerTmall extends AppController 
{
	/**
	 * @var 模型
	 */
	public $mItem;
	
	/**
	 * @var 模型
	 */
	public $mNav;
	
	/**
	 * @var 路由信息
	 */
	public $data;
	
	/**
	 * @var 类型
	 */
	public $type;
	
	/**
	 * @var 当前页码
	 */
	public $p;
	
	/**
	 * @var 
	 */
	public $pinyin;
	
	/**
	 * @var 
	 */
	public $cId;
	
	/**
	 * 构造函数
	 */
	public function __construct()
	{
		parent::__construct();
		
		///
		$this->mItem = M::item();
		$this->mNav = M::nav();
		
		///
		$this->_init();
		
		///
		AppView::addJs('colorbox/colorbox/jquery.colorbox.js');
		AppView::addCss('javascript/colorbox/example5/colorbox.css');
	}
	
	/**
	 * 列表
	 */
	public function actionList()
	{
		switch ($this->type) {
			case 'brand':
				$this->_listBrand();
				break;
		}
	}
	
	
	/// 以下为私有方法 ///
	
	
	/**
	 * 
	 */
	private function _listBrand()
	{
		$default = array('id'=>50100982, 'title'=>'精选品牌', 'pinyin'=>'default');
		$category = $this->mNav->getTmallOne($this->cId);
		if (empty($category)) {
			$category = $default;
			$this->cId = $default['id'];
		}
		if ($this->pinyin != $category['pinyin']) 
			$this->_creatUrl(array('pinyin'=>$category['pinyin']), true);
			
		///
		$data = $this->mItem->getPTmallBrand($this->cId, $this->p);
		$this->_display('tmall/brand', $data, $category);
		
		///
		AppView::addCss('themes/01/zhe.css');
	}
	
	/**
	 * 初始化
	 */
	private function _init()
	{
		$keys = array(
			'p' => 'intval',
			'cid' => 'intval',
			'pinyin' => 'trim',
			'type' => 'trim',
		);
		foreach ($keys as $k => $m) {
			$var = call_user_func_array($m, array($_GET[$k]));
			if ( ! empty($var)) 
				$this->data[$k] = $var;
		}
		///
		$this->p = $this->data['p'];
		$this->cId = $this->data['cid'];
		$this->pinyin = $this->data['pinyin'];
		$this->type = $this->data['type'];
	}
	
	/**
	 * 构造Url
	 */
	private function _creatUrl($data, $h301 = false)
	{
		$params = array_merge($this->data, $data);
		
		///
		$url = Route::urlTmall($params);
		if ($h301) {
			lpSystem::h301($url);
			exit();
		}
		
		///
		return $url;
	}
	
	/**
	 * 输出视图
	 */
	private function _display($template, $data, $category = null)
	{
		list($list, $totalPage, $total) = $data;
		
		///
		$title = $category['title'];
		if ($this->p > 1) 
			$title .= '(第'.$this->p.'页)';
			
		///
		$url = $this->_creatUrl(array('p'=>'%d'));
		$com = App::C('Pagination', $url, $this->p, $totalPage, 8, array(
			'pre' =>'上一页', 
			'next' =>'下一页'));
			
		///
		View::display($template);
		View::assignSet(array(
			'cid' => $this->cId, 
			'total' => $total, 
			'list' => $list, 
			
			///
			'title' => $title, 
			'category' => $category,
			'pagination' => $com->display(),
		));
	}
}
